﻿using System.Diagnostics;
using System.Threading.Tasks;
using AspectCore.DynamicProxy;
using Serilog;

namespace Hicap.AuthorizationServer.Core.Interceptor
{
    public class MethodExecuteLoggerInterceptor : AbstractInterceptor
    {
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            await next(context);
            stopwatch.Stop();
            Log.Information("Executed method {0}.{1}.{2} ({3}) in {4}ms", context.ServiceMethod.DeclaringType.Namespace,
                context.ServiceMethod.DeclaringType.Name,
                context.ServiceMethod.Name,
                context.ServiceMethod.DeclaringType.Assembly.GetName().Name,
                stopwatch.ElapsedMilliseconds);
        }
    }
}